package com.ssh;

public class MyAtoi_8 {
	public static void main(String[] args) {
		String s = "aa-2";
		System.out.println(myAtoi(s));
	}
	
	
	public static int myAtoi(String str) {
		if (str == "" || str.equals("")) {
			return 0;
		}
		int c = 0,s = 0,t = 1;
		for (int i = 0,N = str.length(); i < N; i++) {
			c = str.charAt(i) - '0';
			if (c >= 0 && c <= 9) {
				if (s > Integer.MAX_VALUE / 10|| (s == Integer.MAX_VALUE /10 && Integer.MAX_VALUE % 10 < c)) {
					return s == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
				}else {
					s = s * 10 + c;
				}
				if (s != 0 && i != 0 && str.charAt(i-1) == '-') {
					t = -1;
				}
			}else if (s != 0) {
				break;
			}
		}
		return s * t;
	}
}
